package com.qianya.oa.service.impl;

import com.qianya.oa.common.core.domain.entity.SysUser;
import com.qianya.oa.domain.UserSign;
import com.qianya.oa.mapper.UserSignMapper;
import com.qianya.oa.service.UserSignService;
import com.qianya.oa.vo.UserSignVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserSignServiceImpl implements UserSignService {

    @Resource
    private UserSignMapper userSignMapper;
    @Override
    public List<UserSignVo> getAllUserSigns() {
        return userSignMapper.getAllUserSigns();
    }

    @Override
    public List<UserSignVo> getAllUserSignsByTime(String begin, String end) {
        return userSignMapper.getAllUserSignsByTime(begin, end);
    }

    @Override
    public List<UserSign> getUserSignsByUserId(Integer userId) {
        return userSignMapper.getUserSignsByUserId(userId);
    }

    @Override
    public int addUserSign(UserSign userSign) {
        Date date = new Date();
        userSign.setCreateTime(date);
        LocalDate currentDate = LocalDate.now();
        String formattedDate = currentDate.format(DateTimeFormatter.ISO_DATE);
        userSign.setSignDate(formattedDate);
        LocalTime currentTime = LocalTime.now();
        String formattedTime = currentTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        userSign.setSignTime(formattedTime);
        return userSignMapper.addUserSign(userSign);
    }

    @Override
    public UserSignVo getUserSignById(Integer id) {
        return userSignMapper.getUserSignById(id);
    }

    @Override
    public int updateUserSign(UserSign userSign) {
        return userSignMapper.updateUserSign(userSign);
    }

    @Override
    public int deleteUserSign(Integer id) {
        return userSignMapper.deleteUserSign(id);
    }

    @Override
    public List<Map<String, Object>> getUserSignToday(Integer deptId) {
//        List<Map<String, Object>> pageInfo = new ArrayList<Map<String, Object>>();
//        List<UserSignVo> userSignVos = userSignMapper.teamSignsInfoToday(deptId);
//        List<SysUser> sysUsers = new ArrayList<>();
//        List<UserSignVo> collect = userSignVos.stream().distinct().collect(Collectors.toList());
//        collect.forEach(us->{
//            SysUser ss  = new SysUser();
//            ss.setUserId(Long.valueOf(us.getUserId()));
//            ss.setUserName(us.getUserName());
//            sysUsers.add(ss);
//        });
//        sysUsers.forEach(su->{
//            List<UserSignVo> usvos = new ArrayList<>();
//            Map<String,Object> map = new HashMap<>();
//            userSignVos.forEach(s->{
//                if (Objects.equals(s.getUserId(), Math.toIntExact(su.getUserId()))){
//                    usvos.add(s);
//                }
//            });
//            map.put("user",su);
//            if (usvos.size() > 0){
//                map.put("userSignToday",usvos);
//                map.put("todaySignNumber",usvos.size());
//            }else{
//                map.put("userSignToday",null);
//                map.put("todaySignNumber",0);
//            }
//
//            pageInfo.add(map);
//        });
        return userSignMapper.teamSignsInfoToday(deptId).stream()
                .collect(Collectors.groupingBy(UserSignVo::getUserId))
                .values()
                .stream()
                .map(userSignVos -> {
                    SysUser sysUser = new SysUser();
                    sysUser.setUserId(Long.valueOf(userSignVos.get(0).getUserId()));
                    sysUser.setUserName(userSignVos.get(0).getUserName());
                    Map<String, Object> map = new HashMap<>();
                    map.put("user", sysUser);
                    map.put("userSignToday", userSignVos);
                    map.put("todaySignNumber", userSignVos.get(0).getPosition() == null? 0 : userSignVos.size());
                    return map;
                })
                .collect(Collectors.toList());
    }
}
